package test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import main.Facade;
import main.FacadeDispatcher;
import main.World;
import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.Ambulance;
import unit.FireTruck;
import unit.Garage;
import unit.PoliceCar;
import unit.Unit;
import dispatch.DispatchCenter;
import emergency.*;

public class useCaseTestRemoveUnitAssignment {

	Facade facade;
	Location justALocation;
	
	DispatchCenter center;
	Garage garage;
	
	Emergency em1, em2;
	Unit u1,u2,u3,u4,u5;
	
	@Before
	public void setUp() throws Exception {
		String args[] = new String[1];
		args[0] = "environment.dat";
		World world = new World(args);
		facade = new FacadeDispatcher(world);
		
		center = facade.getDispatchCenter();
		garage = center.getGarage();
		justALocation = new Location(10,10,"NameDoesn'tMatter");
		
		em1 = new Fire(justALocation, Severity.NORMAL, center, Fire.Size.HOUSE, true, 1, 1);
		em2 = new PublicDisturbance(justALocation, Severity.NORMAL, center, 1);

		u1 = new PoliceCar("Policecar-1", 100, garage);
		u2 = new Ambulance("Ambulance-1", 140, garage);
		u3 = new PoliceCar("Policecar-2", 110, garage);
		u4 = new FireTruck("Firetruck-1", 90, garage, Fire.Size.FACILITY);
		u5 = new FireTruck("Firetruck-2", 100, garage, Fire.Size.FACILITY);
	}
	
	@Test (expected=IllegalAccessException.class)
	public void testRevokeInvalidAmbulance() throws IllegalAccessException{
		u2.setCurrentLocation(em2.getLocation());
		u2.assignEmergency(em2);
		u2.revokeUnit();
	}	
	
	@Test (expected=IllegalAccessException.class)
	public void testRevokeInvalidFiretruck() throws IllegalAccessException{
		// can't revoke firetruck 
		u4.revokeUnit();
	}
	
	@Test (expected=IllegalAccessException.class)
	public void testRevokeInvalidPoliceCar() throws IllegalAccessException{
		u1.setCurrentLocation(em1.getLocation());
		u1.assignEmergency(em1);
		u1.revokeUnit();
	}
	
    @Test
	public void testRevokeUnit() throws IllegalAccessException{
    	Garage homeloc = u1.getHomeLocation();
		u1.assignEmergency(em2);
		
		assertEquals(u1.getEmergency(), em2);
		assertTrue(em2.getUnits().contains(u1));
		
		u1.revokeUnit();
		
		//unit revoked
		assertEquals(u1.getEmergency(),null);
		assertFalse(em2.getUnits().contains(u1));
		assertTrue(u1.getCurrentLocation().equals(homeloc.getLocation()));
		
	}
}
